import Vue from 'vue';
import VueSocketIO from "vue-socket.io";
import socketio from 'socket.io-client'

export default function ({ app: { router }, store }) {
  router.afterEach((to, from) => {
    if (from && (from.name === null || from.name === 'users-login')) {
      const auth = store.state.auth;
      const socketUrl = process.env.NODE_ENV === 'production' ? 'https://api.yunliyunwai.cn' : 'http://api.opensource.yundingshuyuan.com'
      if (auth.authenticated) {
        Vue.use(new VueSocketIO({
          debug: false,
          connection: socketio(socketUrl, {
            query: 'token=' +  auth.token
          }),
          options: { path: "/socket.io" }
        }))
      }
    }
  })

}
